//
//  WBNetworkManager.swift
//  WeiBoAPP
//
//  Created by 张学飞 on 2019/1/15.
//  Copyright © 2019 zxf. All rights reserved.
//

import UIKit
import AFNetworking

//枚举
enum AFHTTPMethodType
{
    case GET
    case POST
}

class WBNetworkManager: AFHTTPSessionManager {

    //静态区，常量，闭包
    //第一次访问时，执行闭包，并将结果保存到 shared常量中
    static let  shared:WBNetworkManager = {
       let instace = WBNetworkManager()
        instace.responseSerializer.acceptableContentTypes?.insert("text/plain")
       return instace
    }()
    
    //访问令牌
    //模拟失效
    var accessToken: String? = ""
    
    //封装 AFN的get/post
    
    
    func tokenRequst  (method: AFHTTPMethodType = .GET,
                       URLString: String,parameters:[String:AnyObject]?,
                       completion:@escaping (_ data: Any?, _ isSuccess: Bool)->())  {
        
        guard let token = accessToken else {
            
            completion(nil,false)
            print("没有token，需要登录")
            return
        }
        //转换为可变数组
        var parameters = parameters
        
        if parameters == nil {
            //实例化
            parameters = [String: AnyObject]()
        }
        parameters?["accessToken"] = token as AnyObject
        
        //发起请求
        request(method: method, URLString: URLString, parameters: parameters, completion: completion)
    }
    
    
    //封装AFN的get /Post请求
    func request (method: AFHTTPMethodType = .GET,
                  URLString: String,parameters:[String:AnyObject]?,
                  completion:@escaping (_ data: Any?, _ isSuccess: Bool)->()) {
        
        let success = { (task:URLSessionDataTask,json:Any? )->() in

            completion(json, true)
        }

        let failure = { (task:URLSessionDataTask?,error:Error) ->() in
            if (task?.response as? HTTPURLResponse)?.statusCode == 403{
                print("过期了")
                
                // FIXME: 发送通知，提示用户的登录
            }

            completion(nil,false)
        }
//
//        let progre = { (progress:Progress?) ->() in
//
//
//        }
        
        
        if method == .GET {
            
            get(URLString, parameters: parameters, progress: nil, success: success , failure: failure)
        
        }else {
            post(URLString, parameters: parameters, progress: nil, success: success , failure: failure)}
        
            
//        if method == .GET {
//
//             get(URLString, parameters: parameters, progress: { (progress) in
//
//            }, success: { (task, data) in
//               completion(data,true)
//
//            }) { (task, error) in
//                //失效，？不能参与计算，可以参与比较
//                if (task?.response as? HTTPURLResponse)?.statusCode == 403{
//                    print("过期了")
//
//                    // FIXME: 发送通知，提示用户的登录
//                }
//
//                completion(nil,false)
//            }
//
//        }else {
//            post(URLString, parameters: parameters, progress: { (p ) in
//
//            }, success: { (task, data) in
//                completion(data,true)
//            }) { (task, error) in
//                if (task?.response as? HTTPURLResponse)?.statusCode == 403{
//                    print("过期了")
//                }
//
//                completion(nil,false)
//            }
//        }
//
        
    }
}
